package agent

import (
	"bytes"
	"context"
	"time"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/rpc"
	"github.com/spf13/cast"
	"github.com/xuri/excelize/v2"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ExportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 代理报表导出
func NewExportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ExportLogic {
	return &ExportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ExportLogic) Export(req *types.StatisticRequest) (resp []byte, err error) {
	report, err := l.svcCtx.Analysis.Report(l.ctx, &rpc.AgentAnalysisRequest{
		AgentId:       req.AgentId,
		AgentType:     cast.ToInt32(req.AgentType),
		Level:         cast.ToInt32(req.Level),
		Sort:          req.Sort,
		Day:           req.DateType,
		DurationStart: req.DurationStart,
		DurationEnd:   req.DurationEnd,
		Page:          1,
		PageSize:      5000,
	})
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}
	// 生成excel并提供下载文件
	f := excelize.NewFile()
	defer func() {
		if err := f.Close(); err != nil {
			logc.Error(l.ctx, err)
			return
		}
	}()
	// 创建一个工作表
	index, err := f.NewSheet("Sheet1")
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}
	// 设置单元格的值
	f.SetCellValue("Sheet1", "A1", "会员id")
	f.SetCellValue("Sheet1", "B1", "注册手机号")
	f.SetCellValue("Sheet1", "C1", "上级id")
	f.SetCellValue("Sheet1", "D1", "直接下属")
	f.SetCellValue("Sheet1", "E1", "全部下属")
	f.SetCellValue("Sheet1", "F1", "代理")
	f.SetCellValue("Sheet1", "G1", "注册日期")
	f.SetCellValue("Sheet1", "H1", "最后在线时间")
	f.SetCellValue("Sheet1", "I1", "会员余额")
	f.SetCellValue("Sheet1", "J1", "下单")
	f.SetCellValue("Sheet1", "K1", "下单手续费")
	f.SetCellValue("Sheet1", "L1", "中奖")
	f.SetCellValue("Sheet1", "M1", "佣金")
	f.SetCellValue("Sheet1", "N1", "充值(金额/次)")
	f.SetCellValue("Sheet1", "O1", "提现(金额/次)")
	f.SetCellValue("Sheet1", "P1", "红包")
	f.SetCellValue("Sheet1", "Q1", "充值赠送")
	f.SetCellValue("Sheet1", "R1", "签到")
	f.SetCellValue("Sheet1", "S1", "代理红包")
	f.SetCellValue("Sheet1", "T1", "注册送金")
	f.SetCellValue("Sheet1", "U1", "彩金充值")
	f.SetCellValue("Sheet1", "V1", "首充满赠")
	f.SetCellValue("Sheet1", "W1", "层级")
	// 设置工作簿的默认工作表
	fn := func(a bool, b, c any) any {
		if a {
			return b
		}
		return c
	}
	fnTimeToString := func(t int64) string {
		return time.Unix(t, 64).Format("2006-01-02 15:04:05")
	}
	for _i, _row := range report.Data {
		f.SetCellValue("Sheet1", "A"+cast.ToString(_i+2), _row.UserId)                         // 会员id
		f.SetCellValue("Sheet1", "B"+cast.ToString(_i+2), _row.Phone)                          // 注册手机号
		f.SetCellValue("Sheet1", "C"+cast.ToString(_i+2), _row.ParentId)                       // 上级id
		f.SetCellValue("Sheet1", "D"+cast.ToString(_i+2), _row.ImmediateSubordinate)           // 直接下属
		f.SetCellValue("Sheet1", "E"+cast.ToString(_i+2), _row.AllSubordinate)                 // 全部下属
		f.SetCellValue("Sheet1", "F"+cast.ToString(_i+2), fn(_row.Agent == 1, "内部代理", "外部代理")) // 代理
		f.SetCellValue("Sheet1", "G"+cast.ToString(_i+2), fnTimeToString(_row.RegisterTime))   // 注册日期
		f.SetCellValue("Sheet1", "H"+cast.ToString(_i+2), fnTimeToString(_row.LastOnlineTime)) // 最后在线时间
		f.SetCellValue("Sheet1", "I"+cast.ToString(_i+2), _row.UserBalance)                    // 会员余额
		f.SetCellValue("Sheet1", "J"+cast.ToString(_i+2), _row.Order)                          // 下单
		f.SetCellValue("Sheet1", "K"+cast.ToString(_i+2), _row.OrderFee)                       // 下单手续费
		f.SetCellValue("Sheet1", "L"+cast.ToString(_i+2), _row.Award)                          // 中奖
		f.SetCellValue("Sheet1", "M"+cast.ToString(_i+2), _row.Rebates)                        // 佣金
		f.SetCellValue("Sheet1", "N"+cast.ToString(_i+2), _row.RechargeAvg)                    // 充值(金额/次)
		f.SetCellValue("Sheet1", "O"+cast.ToString(_i+2), _row.WithdrawAvg)                    // 提现(金额/次)
		f.SetCellValue("Sheet1", "P"+cast.ToString(_i+2), _row.RedPacket)                      // 红包
		f.SetCellValue("Sheet1", "Q"+cast.ToString(_i+2), _row.RechargeAward)                  // 充值赠送
		f.SetCellValue("Sheet1", "R"+cast.ToString(_i+2), _row.Sign)                           // 签到
		f.SetCellValue("Sheet1", "S"+cast.ToString(_i+2), _row.AgentRedPacket)                 // 代理红包
		f.SetCellValue("Sheet1", "T"+cast.ToString(_i+2), _row.RegisterAward)                  // 注册送金
		f.SetCellValue("Sheet1", "U"+cast.ToString(_i+2), _row.AwardRecharge)                  // 彩金充值
		f.SetCellValue("Sheet1", "V"+cast.ToString(_i+2), _row.FirstAboveRechargeAward)        // 首充满赠
		f.SetCellValue("Sheet1", "W"+cast.ToString(_i+2), _row.Level)                          // 层级
	}
	f.SetActiveSheet(index)
	var buffer bytes.Buffer
	err = f.Write(&buffer)
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}
	return buffer.Bytes(), nil
}
